package com.sinothk.fc.goodsStock.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.goodsStock.entity.GoodsStockClassifyVO;
import com.sinothk.fc.goodsStock.entity.GoodsStockEntity;
import com.sinothk.fc.goodsStock.entity.GoodsStockSearchReq;
import com.sinothk.fc.goodsStock.service.IGoodsStockService;
import com.sinothk.fc.goodsStockRecord.entity.GoodsStockRecordEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;

 /**
 * @Description: 库存信息
 * @Author: Liangyt
 * @Date:   2022-07-30
 * @Version: V1.0
 */
@Api(tags="库存信息")
@RestController
@RequestMapping("/fc/goodsStockCtrl")
@Slf4j
public class GoodsStockController extends JeecgController<GoodsStockEntity, IGoodsStockService> {
	@Autowired
	private IGoodsStockService goodsStockService;
	
	/**
	 * 分页列表查询
	 *
	 * @param goodsStockEntity
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "库存信息-分页列表查询")
	@ApiOperation(value="库存信息-分页列表查询", notes="库存信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GoodsStockEntity goodsStockEntity,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GoodsStockEntity> queryWrapper = QueryGenerator.initQueryWrapper(goodsStockEntity, req.getParameterMap());
		Page<GoodsStockEntity> page = new Page<GoodsStockEntity>(pageNo, pageSize);
		IPage<GoodsStockEntity> pageList = goodsStockService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param goodsStockEntity
	 * @return
	 */
	@AutoLog(value = "库存信息-添加")
	@ApiOperation(value="库存信息-添加", notes="库存信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GoodsStockEntity goodsStockEntity) {
		return Result.OK(goodsStockService.addOrEdit(goodsStockEntity));
	}
	
	/**
	 *  编辑
	 *
	 * @param goodsStockEntity
	 * @return
	 */
	@AutoLog(value = "库存信息-编辑")
	@ApiOperation(value="库存信息-编辑", notes="库存信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GoodsStockEntity goodsStockEntity) {
		goodsStockService.updateById(goodsStockEntity);
		return Result.OK(goodsStockEntity);
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存信息-通过id删除")
	@ApiOperation(value="库存信息-通过id删除", notes="库存信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		goodsStockService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "库存信息-批量删除")
	@ApiOperation(value="库存信息-批量删除", notes="库存信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.goodsStockService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存信息-通过id查询")
	@ApiOperation(value="库存信息-通过id查询", notes="库存信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GoodsStockEntity goodsStockEntity = goodsStockService.getById(id);
		if(goodsStockEntity ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(goodsStockEntity);
	}

//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param goodsStockEntity
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, GoodsStockEntity goodsStockEntity) {
//        return super.exportXls(request, goodsStockEntity, GoodsStockEntity.class, "库存信息");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, GoodsStockEntity.class);
//    }

	 @AutoLog(value = "分页列表查询")
	 @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
	 @GetMapping(value = "/storeGoodsStockPage")
	 public Result<?> storeGoodsStockPage(GoodsStockEntity goodsStockEntity,
									 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
									 HttpServletRequest req) throws Exception {
		 IPage<GoodsStockEntity> pageList = goodsStockService.storeGoodsStockPage(new Page<>(pageNo, pageSize), goodsStockEntity);
		 return Result.OK(pageList);
	 }

	 /**
	  * 分页列表查询
	  *
	  * @param searchReq
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @AutoLog(value = "分页列表-归类查询")
	 @ApiOperation(value = "分页列表-归类查询", notes = "分页列表-归类查询")
	 @GetMapping(value = "/listForClassify")
	 public Result<?> listForClassify(GoodsStockSearchReq searchReq,
									  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
		 IPage<GoodsStockClassifyVO> pageList = goodsStockService.listForClassify(new Page<>(pageNo, pageSize), searchReq);
		 return Result.OK(pageList);
	 }

	 /**
	  * 查询同一条码产品
	  *
	  * @param searchReq
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @AutoLog(value = "分页列表-查询同一条码产品")
	 @ApiOperation(value = "分页列表-查询同一条码产品", notes = "分页列表-查询同一条码产品")
	 @GetMapping(value = "/listForBarCode")
	 public Result<?> listForBarCode(GoodsStockSearchReq searchReq,
									 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
		 IPage<GoodsStockEntity> pageList = goodsStockService.listForBarCode(new Page<>(pageNo, pageSize), searchReq);
		 return Result.OK(pageList);
	 }

	 /**
	  *  批量删除
	  *
	  * @param barcode
	  * @return
	  */
	 @AutoLog(value = "库存信息-批量删除")
	 @ApiOperation(value="库存信息-批量删除", notes="库存信息-批量删除")
	 @DeleteMapping(value = "/deleteBathStockForBarcode")
	 public Result<?> deleteBathStockForBarcode(@RequestParam(name="barcode") String barcode) throws Exception {
		 this.goodsStockService.deleteBathStockForBarcode(barcode);
		 return Result.OK("批量删除成功!");
	 }

	 /**
	  * 通过Barcode查询
	  *
	  * @param barcode
	  * @return
	  */
	 @AutoLog(value = "库存信息-通过Barcode查询")
	 @ApiOperation(value="库存信息-通过Barcode查询", notes="库存信息-通过Barcode查询")
	 @GetMapping(value = "/getStockTotalInfoForBarcode")
	 public Result<?> getStockTotalInfoForBarcode(@RequestParam(name="barcode") String barcode) throws Exception {
		 GoodsStockEntity goodsStockEntity = goodsStockService.getStockTotalInfoForBarcode(barcode);
		 if(goodsStockEntity ==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(goodsStockEntity);
	 }

	 /**
	  * 同款补货
	  *
	  * @param beanReq
	  * @return
	  */
	 @AutoLog(value = "商品库存表-同款补货")
	 @ApiOperation(value = "商品库存表-同款补货", notes = "商品库存表-同款补货")
	 @PostMapping(value = "/addStockNum")
	 public Result<GoodsStockRecordEntity> addStockNum(@RequestBody GoodsStockEntity beanReq) throws Exception {
		 return Result.OK(goodsStockService.addStockNum(beanReq));
	 }

	 /**
	  * 添加-批量
	  *
	  * @param stockList
	  * @return
	  */
	 @AutoLog(value = "商品库存表-批量添加")
	 @ApiOperation(value = "商品库存表-批量添加", notes = "商品库存表-批量添加")
	 @PostMapping(value = "/addBatch")
	 public Result<ArrayList<GoodsStockRecordEntity>> addBatch(@RequestBody ArrayList<GoodsStockEntity> stockList) throws Exception {
		 ArrayList<GoodsStockRecordEntity> storeStockRecordList = goodsStockService.addBatch(stockList);
		 return Result.OK(storeStockRecordList);
	 }
}
